﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.member
{
    public partial class product_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(product_add));
            var userInfo = WebMasterCookie.GetCookie();
            if (!IsPostBack)
            {
                if (userInfo == null)
                {
                    Response.Redirect("/login.aspx");
                }
            }
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string OnSaveProduct(string json)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo!=null)
            {
                try
                {
                    var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                    dicProj.Add("client_id", userInfo.Id);
                    if (dicProj["proid"].ToString() == "")
                    {
                        var dt = ClientInfoBll.GetClientById(userInfo.Id, CommonConst.DB_Name);
                        if (dt.Rows.Count > 0)
                        {
                            //判断当前用户积分
                            var points = int.Parse(dt.Rows[0]["User_Points"].ToString());
                            if (points > CommonConst.gLosePoints)
                            {
                                //添加
                                int count = ProductInfoBll.InsertProductInfo(dicProj, CommonConst.DB_Name);
                                //添加一条产品扣除相应积分
                                ClientInfoBll.UpdateClientPoints(userInfo.Id, CommonConst.gLosePoints, "-", CommonConst.DB_Name);
                                if (count > 0)
                                {
                                    jsonResult.Add("isreload", true);
                                    jsonResult.Add("msg", "产品添加成功!");
                                }
                                else
                                {
                                    jsonResult.Add("isreload", false);
                                    jsonResult.Add("msg", "产品添加失败!");
                                }
                            }
                            else
                            {
                                jsonResult.Add("isreload", false);
                                jsonResult.Add("msg", "您的积分不够，不能继续添加产品!");
                            }
                        }
                        else
                        {
                            jsonResult.Add("isreload", false);
                            jsonResult.Add("msg", "登录超时，请退出重新登录!");
                        }
                    }
                    else
                    {
                        //修改
                        var proid = EncryptHelper.DecryptString(dicProj["proid"].ToString());
                        dicProj.Remove("proid");
                        dicProj.Add("proid", proid);
                        int count = ProductInfoBll.UpdateProductInfo(dicProj, CommonConst.DB_Name);
                        if (count > 0)
                        {
                            Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                            dicHandleLog.Add("user_id", userInfo.Id);
                            dicHandleLog.Add("computername", userInfo.Computername);
                            dicHandleLog.Add("computerip", userInfo.Computerip);
                            dicHandleLog.Add("handle_note", "修改产品");
                            dicHandleLog.Add("obj_guid", "Product_Id=" + dicProj["proid"].ToString());
                            LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                            jsonResult.Add("isreload", true);
                            jsonResult.Add("msg", "产品修改成功!");
                        }
                        else
                        {
                            jsonResult.Add("isreload", false);
                            jsonResult.Add("msg", "产品修改失败!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //获取产品分类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProInfo(string GC_Id)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=\"\">--请选择--</option>");
            try
            {
                if (GC_Id.Trim() != "")
                {
                    var dt = ProductInfoBll.GetGoodsInfo(GC_Id,true, CommonConst.DB_Name);
                    foreach (DataRow row in dt.Rows)
                    {
                        builder.AppendFormat("<option value=\"{0}\">{1}</option>", row["G_Id"], row["G_Name"]);
                    }
                }
                jsonResult.Add("isreload",true);
                jsonResult.Add("msg",builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //查看商品大类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProClass(string parendId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=\"\">--请选择--</option>");
            try
            {
                if (parendId.Trim() != "")
                {
                    var dt = ProductInfoBll.GetGoodsClass(parendId, CommonConst.DB_Name);
                    foreach (DataRow row in dt.Rows)
                    {
                        builder.AppendFormat("<option value=\"{0}\">{1}</option>", row["GC_Id"], row["GC_Name"]);
                    }
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //获取商品归类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProCategory()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=\"\">--请选择--</option>");
            try
            {
                var dt = ProductInfoBll.GetGoodsCategory("2", CommonConst.DB_Name);
                foreach (DataRow row in dt.Rows)
                {
                    builder.AppendFormat("<option value=\"{0}\">{1}</option>", row["GC_Id"], row["GC_Name"]);
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //获取商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProductInfo(string proId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo!=null)
            {
                try
                {
                    proId = EncryptHelper.DecryptString(proId);
                    var tb = ProductInfoBll.GetProductInfoTable(" Product_Id=" + proId, " Product_Id asc", CommonConst.DB_Name);
                    if (tb.Rows.Count!=0)
                    {
                        jsonResult.Add("isreload",true);
                        jsonResult.Add("G_Id", tb.Rows[0]["G_Id"]);
                        jsonResult.Add("Product_Title", tb.Rows[0]["Product_Title"]);
                        jsonResult.Add("Product_Picture", tb.Rows[0]["Product_Picture"]);
                        jsonResult.Add("province", tb.Rows[0]["provinceID"]);
                        jsonResult.Add("city", tb.Rows[0]["cityID"]);
                        jsonResult.Add("area", tb.Rows[0]["areaID"]);
                        jsonResult.Add("detail", tb.Rows[0]["Product_Place"]);
                        jsonResult.Add("Product_EndTime", Convert.ToDateTime(tb.Rows[0]["Product_EndTime"]).ToString("yyyy-MM-dd"));
                        jsonResult.Add("Product_Remark", tb.Rows[0]["Product_Remark"]);
                        jsonResult.Add("Product_Amount", tb.Rows[0]["Product_Amount"]);
                        jsonResult.Add("proInfo", tb.Rows[0]["G_Id"]);
                        jsonResult.Add("unit", tb.Rows[0]["Product_Unit"]);
                        var dt = ProductInfoBll.GetGoodsinfobyId(tb.Rows[0]["G_Id"].ToString(), CommonConst.DB_Name);
                        jsonResult.Add("classInfo", dt.Rows[0]["GC_Id"]);
                        var dd = ProductInfoBll.GetGoodsClassbyId(dt.Rows[0]["GC_Id"].ToString(), CommonConst.DB_Name);
                        jsonResult.Add("classId", dd.Rows[0]["GC_PId"]);
                        //产品图片
                        var dt1 = ProductInfoBll.GetProductPic(proId);
                        StringBuilder builder=new StringBuilder();
                        foreach (DataRow row in dt1.Rows)
                        {
                            builder.AppendFormat("|{0}", row["Pic_Path"]);
                        }
                        if (builder.ToString().Length>0)
                        {
                            jsonResult.Add("pic",builder.ToString().Substring(1));
                        }
                    }
                    else
                    {
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "该产品不存在或者已被删除！");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
                
            }
            else
            {
                jsonResult.Add("isreload",false);
                jsonResult.Add("msg","登录超时，请重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //获取地区信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetArea(int type, string pId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=\"\">--请选择--</option>");
            try
            {
                var dt = ProductInfoBll.GetAreaList(type, pId, CommonConst.DB_Name);
                string value = "provinceID", text = "province";
                switch (type)
                {
                    case 0:
                        value = "provinceID";
                        text = "province";
                        break;
                    case 1:
                        value = "cityID";
                        text = "city";
                        break;
                    case 2:
                        value = "areaID";
                        text = "area";
                        break;
                }
                if (dt!=null)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        builder.AppendFormat("<option value=\"{0}\">{1}</option>", row[value], row[text]);
                    }
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
